package com.cs410.collabwriting.servlets;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cs410.collabwriting.connectors.SessionManager;
import com.cs410.collabwriting.main.Story;
import com.cs410.collabwriting.main.StoryManager;
import com.cs410.collabwriting.main.TagConstant;
import com.cs410.collabwriting.main.User;
import com.cs410.collabwriting.tools.DatabaseManager;

/**
 * Servlet implementation class PageHandler
 */
@WebServlet("/home")
public class PageHandler extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PageHandler() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String sessionID = request.getRequestedSessionId();
		User user = SessionManager.getUserIDForJSessionID( sessionID );
		Enumeration<String> attName = request.getAttributeNames();
		while( attName.hasMoreElements() ) {
			String name = attName.nextElement();
			request.setAttribute( name, request.getAttribute( name ) );
		}
		if( DatabaseManager.initializeDatabaseManager() != null ) {
			request.setAttribute("redirected_from_home", "true");
			if( user != null ) {
				request.setAttribute("userid", user.getUserID());
				request.setAttribute("biography", user.getBiography());
				request.setAttribute("datecreated", user.getDateCreated());
				request.setAttribute("email", user.getEmailAddress());
				request.setAttribute("firstname", user.getFirstName());
				request.setAttribute("gender", user.getGender());
				request.setAttribute("lastname", user.getLastName());
				request.setAttribute("lastupdated", user.getLastUpdate());
				request.setAttribute("location", user.getLocation());
			}
			String storyid = request.getParameter("storyid");
			if( storyid != null && !storyid.isEmpty() ) {
				Story story = StoryManager.getInstance().getStoryByID( Integer.parseInt( storyid ) );
				request.setAttribute("storyid", storyid );
				request.setAttribute("storytitle", story.getTitle() );
				story.incrementViewCount();
			}
			
			String[] tagConstants = TagConstant.getAllTagNames();
			request.setAttribute("tags", tagConstants );
			String pageName = request.getParameter("page");
			if( pageName != null ) {
				if( !pageName.contains( ".jsp") ) {
					pageName += ".jsp";
				}
			} else {
				pageName = "index.jsp";
			}
			RequestDispatcher dispatcher = request.getRequestDispatcher(pageName);
			if( dispatcher != null ) {
				dispatcher.forward(request, response);
			}
		} else {
			response.getWriter().print("ERROR: WebSite Temporarily Unavailable");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
